package com.xiaoqi.api.controller.admin;

import com.github.pagehelper.util.StringUtil;
import com.xiaoqi.api.constant.StatusConst;
import com.xiaoqi.api.dto.EngineerDTO;
import com.xiaoqi.api.exception.ParamException;
import com.xiaoqi.api.pojo.Engineer;
import com.xiaoqi.api.properties.JWTProperties;
import com.xiaoqi.api.query.EngineerQuery;
import com.xiaoqi.api.service.EngineerService;
import com.xiaoqi.api.utlis.JWTUtil;
import com.xiaoqi.api.utlis.PageResult;
import com.xiaoqi.api.utlis.Result;
import com.xiaoqi.api.vo.EngineerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "工程师登录")
@Slf4j
@RestController
@RequestMapping("/admin/engineer")
public class EngineerController {

    @Resource
    EngineerService engineerService;

    @Resource
    JWTProperties jwtProperties;

    /**
     * 用户登录
     * @param loginName
     * @param password
     * @return
     */
    @ApiOperation("工程师")
    @PostMapping("/login")
    public Result<List<EngineerVO>> Login(String loginName, String password) throws Exception {
        log.info("用户登录："+"账号："+loginName+",密码："+password);
        //参数的校验
        if(StringUtil.isEmpty(loginName) || StringUtil.isEmpty(password)){
            throw new ParamException("账号和密码都不能为空");
        }
        EngineerVO engineerVO = engineerService.login(loginName,password);
        if(engineerVO==null){
            return Result.failure("账号或密码错误");
        }
        Map<String,Object> payload = new HashMap<>();
        payload.put(JWTUtil.ENGINEER_ID,engineerVO.getId());
        String token = JWTUtil.generateToken(payload,jwtProperties.getSecure(),jwtProperties.getExpire());
        log.info("token："+token);
        Map<String,Object> data = new HashMap<>();
        data.put("token",token);
        data.put("engineerInfo",engineerVO);
        return Result.ok("成功",data);
    }

    @ApiOperation("添加工程师")
    @PostMapping
    public Result add(EngineerDTO engineerDTO){
        engineerService.add(engineerDTO);
        return Result.ok("添加成功");
    }
    

    @ApiOperation("查询工程师")
    @GetMapping
    public Result<List<EngineerVO>> find(EngineerQuery engineerQuery){ //engineerQuery封装了查询的条件
        PageResult<EngineerVO> page = engineerService.find(engineerQuery);
        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("根据id查询工程师")
    @GetMapping("/{id}")
    public Result findById(@PathVariable long id){
        EngineerVO engineerVO = engineerService.findById(id);
        return Result.ok("查询成功",engineerVO);
    }



    @ApiOperation("编辑工程师")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id,EngineerDTO engineerDTO){ //@RequestBody将前端传递的json格式的请求参数转为对象
        engineerService.update(id,engineerDTO);
        return Result.ok("修改成功");
    }



    @ApiOperation("禁用启动")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            engineerService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            engineerService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            //throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }

}
